<template>
	<view class="bg-white login min-h-full flex flex-col items-center px-[40rpx] pt-[80rpx] box-border">
		<view>
			<u-image :src="appStore.getWebsiteConfig.shop_logo" mode="widthFix" height="160" width="160" />
		</view>
		<view class="mt-4 text-xl font-medium">{{ appStore.getWebsiteConfig.shop_name }}</view>
		<view class="w-full mt-[60rpx] pb-[60rpx]">

			<view class="mt-[400rpx]">
				<u-checkbox shape="circle" v-model="isCheckAgreement" >
					<view class="text-xs flex">
						     已阅读并同意
						<view @click.stop>
							<navigator class="text-primary" hover-class="none"
								url="/pages/agreement/agreement?type=service">
								《服务协议》
							</navigator>
						</view>

						和
						<view @click.stop>
							<navigator class="text-primary" hover-class="none"
								url="/pages/agreement/agreement?type=privacy">
								《隐私协议》
							</navigator>
						</view>
					</view>
				</u-checkbox>
			</view>
			<view class="mt-[60rpx]">
				<!-- @click="handleLogin(formData.scene)" -->
				<!-- @click="wxLogin" -->
				<view class="login-btn">
					<button open-type="getPhoneNumber" @getphonenumber="getPhoneNumber" class="button_pos"></button>
					一 键 登 录
				</view>
				<!-- <u-button type="primary" shape="circle" @click="handleLogin(formData.scene)">
                   
                </u-button> -->
			</view>
		</view>
		<mplogin-popup v-model:show="showLoginPopup" :logo="websiteConfig.shop_logo" :title="websiteConfig.shop_name"
			@update="handleUpdateUser" />
	</view>
</template>

<script setup lang="ts">
	import { mnpLogin, updateUser, getOpenId,mobileLogin } from '@/api/account'

	import { BACK_URL } from '@/enums/constantEnums'

	import { useAppStore } from '@/stores/app'
	import { useUserStore } from '@/stores/user'
	import cache from '@/utils/cache'


	import { onLoad, onShow } from '@dcloudio/uni-app'
	import { computed, reactive, ref, shallowRef, watch } from 'vue'




	const userStore = useUserStore()
	const appStore = useAppStore()


	const showLoginPopup = ref(false)
	const isCheckAgreement = ref(false)



	const loginData = ref()


	const websiteConfig = computed(() => appStore.getWebsiteConfig)







	const isOpenAgreement = computed(() => appStore.getLoginConfig.login_agreement == 1)

	const isForceBindMobile = computed(() => appStore.getLoginConfig.coerce_mobile == 1)




	const loginHandle = async (data : any) => {
		const { token, mobile } = data

		userStore.login(data.token)
		await userStore.getUser()
		uni.$u.toast('登录成功')
		uni.hideLoading()
		const pages = getCurrentPages()
		if (pages.length > 1) {
			const prevPage = pages[pages.length - 2]
			uni.navigateBack({
				success: () => {
					// @ts-ignore
					const { onLoad, options } = prevPage
					// 刷新上一个页面
					onLoad && onLoad(options)
				}
			})
		} else if (cache.get(BACK_URL)) {
			uni.redirectTo({ url: cache.get(BACK_URL) })
		} else {
			uni.reLaunch({
				url: '/pages/index/index'
			})
		}
		cache.remove(BACK_URL)
	}






	const getOpenIdEnv = async () => {
		const { code } : any = await uni.login({
			provider: 'weixin'
		})
		const data = await getOpenId({
			code: code
		});
		uni.setStorageSync("OPENID", data.key);
	}

	const getPhoneNumber = async (e) => {
if (!isCheckAgreement.value ){
	     return uni.$u.toast('请勾选已阅读并同意《服务协议》和《隐私协议》')
}
   
		uni.showLoading({
			title: '请稍后...'
		})
		try {
			const { encryptedData, iv, code } = e.detail
			const data = {
				code,
				encrypted_data: encryptedData,
				iv:iv,
				key:uni.getStorageSync("OPENID"),
				friend_id:uni.getStorageSync("MYID"),
			}
			if (encryptedData) {
				let retu = await mobileLogin({
					...data
				})
				loginHandle(retu)
			}
		
		} catch (error : any) {
			uni.hideLoading()
			uni.$u.toast(error)
		}


	}
	const handleUpdateUser = async (value : any) => {
		await updateUser(value, { token: userStore.temToken })
		showLoginPopup.value = false
		loginHandle(loginData.value)
	}

	watch(
		() => appStore.getLoginConfig,
		(value) => {
			if (value.login_way) {
				// formData.scene = value.login_way[0]
			}
		},
		{
			immediate: true
		}
	)

	onShow(async () => {
		try {
			if (userStore.isLogin) {
				uni.showLoading({
					title: '请稍后...'
				})
				await userStore.getUser()
				uni.hideLoading()
				uni.navigateBack()
			}
		} catch (error : any) {
			uni.hideLoading()
		}
		getOpenIdEnv();
	})

	onLoad(async () => {
		if (userStore.isLogin) {
			// 已经登录 => 首页
			uni.reLaunch({
				url: '/pages/index/index'
			})
			return
		}
	})
</script>

<style lang="scss">
	.button_pos {
		position: absolute;
		top: 0;
		left: 0;
		width: 100%;
		height: 100%;
		opacity: 0;
		z-index: 2;
		background-color: red;
	}

	page {
		height: 100%;
	}

	.login-btn {
		position: relative;
		height: 78rpx;
		background-color: #FF4907;
		 // background: linear-gradient(180deg, #F7DA1C 0%, #ffc200 100%);
		// box-shadow: 0rpx 10rpx 30rpx 0rpx rgba(41, 255, 154, 0.5);
		border-radius: 39rpx;
		text-align: center;
		line-height: 78rpx;
		font-weight: 400;
		color: #FFFFFF;
		font-size: 30rpx;
	}
</style>